import GlobalManager from "../../resources/util/globlaManager"
const topUpArr = [20,100,500,1000,5000,10000,20000,50000,80000,100000]
cc.Class({
    extends: cc.Component,

    properties: {
        head: cc.Sprite,
        changePswPanel: cc.Node,
        vipLabel : cc.Label,
        vipLabel2 : cc.Label,
        vipLabel3 : cc.Label,

        rechargeLabel : cc.Label,
        rechargeJindu : cc.ProgressBar,
    },


    //只调用一次
    // onLoad() {

    // },


    //每次显示时调用
    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.playerInfo = this.lobbyMain.playerInfo;
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.sound = this.playerInfo.sound;
        this.node.getChildByName("headPhoto").active = false;
        this.left = this.node.getChildByName("left");
        this.right = this.node.getChildByName("right");

        //ID
        this.idLabel = this.right.getChildByName("id").getChildByName("label").getComponent(cc.Label);
        this.idLabel.string = this.playerInfo.playerId;
        //name
        this.right.getChildByName("name").getChildByName("label").getComponent(cc.Label).string = this.playerInfo.playerName;
        //balance
        this.right.getChildByName("balance").getChildByName("label").getComponent(cc.Label).string = (this.playerInfo.playerCoin).toFixed(2);
        //音效按钮位置
        this.soundNode = this.left.getChildByName("sound").getChildByName("set_soundBg")
        //vip
        this.vipLabel.string = this.playerInfo.vip;
        this.vipLabel2.string = this.playerInfo.vip;
        this.vipLabel3.string = this.playerInfo.vip + 1;

        //充值数
        this.rechargeLabel.string = this.playerInfo.totalRecharge
        this.handleTopUpProgress();  //充值进度

        // 加载头像
        const imagePath = "image/head/tx_head" + this.playerInfo.playerHead;
        let self = this;
        const bundle = cc.assetManager.getBundle("hall");
        if (!bundle) {
            cc.error("未找到 bundle：hall");
            return;
        }
        bundle.load(imagePath, cc.SpriteFrame, (err, spriteFrame) => {
            if (err) {
                cc.error("加载头像失败：" + (err.message || err));
                return;
            }
            self.head.spriteFrame = spriteFrame;
        });


        this.bindAccPanel = this.node.getChildByName("bindAccount");
        this.bindAccPanel.active = false;

    },

    start() {
        this.username = ""
        this.password = ""
        if (this.sound == "true") {
            this.soundNode.getChildByName("On").active = true;
            this.soundNode.getChildByName("Off").active = false;
        } else {
            this.soundNode.getChildByName("On").active = false;
            this.soundNode.getChildByName("Off").active = true;
        }

        this.node.getChildByName("panel_version").getComponent(cc.Label).string = GlobalManager.panel_version;
        this.right.getChildByName("bindAccount").getChildByName("btn_mai").active = this.playerInfo.isGuest;
        this.right.getChildByName("bindAccount").getChildByName("label").active = !this.playerInfo.isGuest;
    },

    onClick(event, target) {
        if (target == "sound") {
            this.setSound();
            this.lobbyMain.playClickSound();
        }
        this.lobbyMain.playClickSound();
        switch (target) {
            case "headphoto":  // 头像
                this.openHeadPhoto();
                break;
            case "returnLogin":  // 返回登录
                this.lobbyMain.returnLogin();
                break;
            case "savehead":  // 保存头像
                this.savaHead();
                break;
            case "changepassword":  // 打开修改密码界面
                this.changePassword();
                break;
            case "changePsw":  // 修改密码
                this.changePsw();
                break;
            case "close":  // 修改密码
                this.lobbyMain.hidePanel(this.node);
                break;
            case "copyId":  // 复制Id
                this.lobbyMain.copyText(this.idLabel.string);
                break;
            case "bindAccount":  // 绑定账号
                this.openBindPanel();
                break;
            case "goBindAccount":  // 确认绑定账号
                this.goBindAccount();
                break;
            case "shop":  // 充值
                this.lobbyMain.openShopPanel();
                break;
            default:
                break;
        }
    },


    //充值进度
    handleTopUpProgress() { 
        let target = 0
        for (let i = 0; i < topUpArr.length; i++) {
            const element = topUpArr[i];
            if (this.playerInfo.totalRecharge < element) {
                target = element;
                break
            }
        }
        let fen = (this.playerInfo.totalRecharge / target).toFixed(2)
        this.rechargeLabel.string = this.playerInfo.totalRecharge + "/" + target
        this.rechargeJindu.progress = fen
    },

    // 绑定账号返回
    onbindAccount(data) {
        if (data.respCode == "SUCCESS") {
            this.lobbyMain.showTips("Binding successful.");
            this.lobbyMain.hidePanel(this.bindAccPanel);
            this.playerInfo.writeToCache("userName", this.username)
            this.playerInfo.writeToCache("passWord", this.password)
            this.playerInfo.isGuest = 0;
            this.right.getChildByName("bindAccount").getChildByName("btn_mai").active = this.playerInfo.isGuest;
            this.right.getChildByName("bindAccount").getChildByName("label").active = !this.playerInfo.isGuest;
        } else {
            this.lobbyMain.showTips(data.message);
        }
    },

    // 绑定账号
    goBindAccount() {
        let account = this.bindAccPanel.getChildByName("EditBox1").getComponent(cc.EditBox).string;
        let password = this.bindAccPanel.getChildByName("EditBox2").getComponent(cc.EditBox).string;
        let Password_confirm = this.bindAccPanel.getChildByName("EditBox3").getComponent(cc.EditBox).string;
        if (account == "" || password == "" || Password_confirm == "") {
            this.lobbyMain.showTips("Please fill in the account and password.");
            return;
        } else if (password != Password_confirm) {
            this.lobbyMain.showTips("The two passwords are inconsistent.");
            return;
        }

        password = this.lobbyMain.forMd5(password);
        this.username = account
        this.password = password
        let data = {
            "action": "bindAccount",
            "playerId": this.playerInfo.playerId,
            "username": account,
            "password": password,
        }
        this.lobbyNet.sendMessage(data);

    },

    // 打开绑定账号
    openBindPanel() {
        if (this.bindAccPanel.active) {
            this.lobbyMain.hidePanel(this.bindAccPanel);
        } else {
            this.lobbyMain.showPanel(this.bindAccPanel);
        }
    },


    // 修改密码响应
    onChangePsw(data) {
        if (data.respCode == "SUCCESS") {
            this.lobbyMain.showTips("Password changed successfully.");
            this.lobbyMain.hidePanel(this.changePswPanel);
            this.lobbyMain.returnLogin();
        } else {
            this.lobbyMain.showTips(data.data);
        }
    },

    // 修改密码
    changePsw() {
        let oldPsw = this.changePswPanel.getChildByName("oldPsw").getComponent(cc.EditBox).string;
        let newPsw = this.changePswPanel.getChildByName("newPsw").getComponent(cc.EditBox).string;
        let pswAgain = this.changePswPanel.getChildByName("pswAgain").getComponent(cc.EditBox).string;
        if (oldPsw == "" || newPsw == "" || pswAgain == "") {
            this.lobbyMain.showTips("Please fill in the password.");
            return;
        } else if (newPsw != pswAgain) {
            this.lobbyMain.showTips("The two passwords are inconsistent.");
            return;
        }

        oldPsw = this.lobbyMain.forMd5(oldPsw);
        newPsw = this.lobbyMain.forMd5(newPsw);

        let data = {
            "action": "changePsw",
            "playerId": this.playerInfo.playerId,
            "oldPsw": oldPsw,
            "newPsw": newPsw,
        }
        this.lobbyNet.sendMessage(data);
    },

    // 修改密码界面
    changePassword() {
        if (this.changePswPanel.active) {
            this.lobbyMain.hidePanel(this.changePswPanel);
        } else {
            this.lobbyMain.showPanel(this.changePswPanel);
        }
    },


    // 点击头像
    changeHead(event, head) {
        this.lobbyMain.playClickSound();
        this.headId = head;
    },

    // 保存头像
    savaHead() {
        let data = {
            "action": "changeHead",
            "headId": this.headId,
            "playerId": this.playerInfo.playerId,
        }
        this.lobbyNet.sendMessage(data);
    },

    //修改头像后
    onChangeHeadResponse(data) {
        if (data.respCode == "SUCCESS") {
            this.playerInfo.playerHead = this.headId;
            this.playerInfo.writeToCache("playerHead", this.playerInfo.playerHead);
            this.lobbyMain.loadHeadPhoto();
            const imagePath = "image/head/tx_head" + this.playerInfo.playerHead;
            let self = this;
            const bundle = cc.assetManager.getBundle("hall");
            if (!bundle) {
                cc.error("未找到 bundle：hall");
                return;
            }
            bundle.load(imagePath, cc.SpriteFrame, (err, spriteFrame) => {
                if (err) {
                    cc.error("加载头像失败：" + (err.message || err));
                    return;
                }
                if (!self.head) return;
                self.head.spriteFrame = spriteFrame;
            });
            this.lobbyMain.hidePanel(this.node.getChildByName("headPhoto"));
            this.lobbyMain.showTips("Avatar updated successfully.");
        } else {
            this.lobbyMain.showTips("Avatar update failed.");
        }

    },

    // 打开头像
    openHeadPhoto() {
        let headPhotoPanel = this.node.getChildByName("headPhoto")

        if (headPhotoPanel.active == false) {
            this.lobbyMain.showPanel(headPhotoPanel);
        } else {
            this.lobbyMain.hidePanel(headPhotoPanel);
            return;
        }

        let head_parent = headPhotoPanel.getChildByName("signIn_ruleBg").getChildByName("ScrollView").getChildByName("view").getChildByName("content");
        let head_list = head_parent.children;
        let headName = "tx_head" + this.playerInfo.playerHead;
        for (let i = 0; i < head_list.length; i++) {
            const element = head_list[i];
            if (element.name == headName) {
                element.getComponent(cc.Toggle).isChecked = true;
                break;
            }
        }
    },



    // 设置音效
    setSound() {
        if (this.sound == "true") {
            this.soundNode.getChildByName("On").active = false;
            this.soundNode.getChildByName("Off").active = true;
            this.sound = "false";
            this.playerInfo.sound = this.sound;
        } else {
            this.soundNode.getChildByName("On").active = true;
            this.soundNode.getChildByName("Off").active = false;
            this.sound = "true";
            this.playerInfo.sound = this.sound;
        }
        this.playerInfo.writeToCache("sound", this.sound);
        this.lobbyMain.playBgm();
    },
});
